March 28, 2016
That data we will be using today is available at
Download this file onto your computer. The codebook can also be found here.
The slides can be viewed at
All source files can be found at
movies <- read.csv("data/movies.csv", stringsAsFactors = FALSE)
ggplot2 is a plotting system for R, based on the grammar of graphics, which tries to take the good parts of base and lattice graphics and none of the bad parts.
It takes care of many of the fiddly details that make plotting a hassle (like drawing legends) as well as providing a powerful model of graphics that makes it easy to produce complex multi-layered graphics.
Source: http://ggplot2.org/
Statistical: Visualize \(Y \sim X | Z\) with ease
Technical: Human readable syntax
Bonus: Aesthetically pleasing
ggplot(data = movies, aes(x = critics_score, y = audience_score, color = title_type)) + geom_point() + geom_smooth(method = "lm", se = FALSE)
plot(y = movies$audience_score, x = movies$critics_score,
col = factor(movies$title_type))
doc <- movies[movies$title_type == "Documentary", ]
ff <- movies[movies$title_type == "Feature Film", ]
tv <- movies[movies$title_type == "TV Movie", ]
m_doc <- lm(audience_score ~ critics_score, data = doc)
m_ff <- lm(audience_score ~ critics_score, data = ff)
m_tv <- lm(audience_score ~ critics_score, data = tv)
abline(m_doc, col = 1)
abline(m_ff, col = 2)
abline(m_tv, col = 3)
legend("topleft", levels(factor(movies$title_type)),
col = c(1,2,3), lty = 1)
ggplot2 package
A statistical graphic is a…
ggplot(data = movies, aes(x = audience_score, y = critics_score)) + geom_point()
ggplot(data = movies, aes(x = audience_score, y = critics_score)) + geom_point(alpha = 0.5, color = "blue")
ggplot(data = movies, aes(x = audience_score, y = critics_score, color = genre)) + geom_point(alpha = 0.5) + facet_grid(. ~ title_type)
ggplot(data = movies, aes(x = audience_score, y = critics_score, color = genre)) + geom_point(alpha = 0.5) + facet_grid(audience_rating ~ title_type)
ggplot(data = [dataframe], aes(x = [var_x], y = [var_y],
color = [var_for_color], fill = [var_for_fill],
shape = [var_for_shape])) +
geom_[some_geom] +
... # other options
ggplot(data = movies, aes(x = audience_score)) + geom_histogram(binwidth = 5)
ggplot(data = movies, aes(y = audience_score, x = genre)) + geom_boxplot()
ggplot(data = movies, aes(y = audience_score, x = genre)) + geom_boxplot() + theme(axis.text.x=element_text(angle = 45, hjust = 1))
ggplot(data = movies, aes(x = runtime, color = audience_rating)) + geom_density()
ggplot(data = movies, aes(x = runtime, fill = audience_rating)) + geom_density()
ggplot(data = movies, aes(x = runtime, fill = audience_rating)) + geom_density(alpha = 0.5)
ggplot(data = movies, aes(x = imdb_rating, y = audience_score)) + geom_point(alpha = 0.5)
ggplot(data = movies, aes(x = imdb_rating, y = audience_score)) + geom_point(alpha = 0.5) + geom_smooth()
ggplot(data = movies, aes(x = imdb_rating, y = audience_score)) + geom_point(alpha = 0.5) + geom_smooth(method = "lm")
ggplot(data = movies, aes(x = genre)) + geom_bar() + theme(axis.text.x=element_text(angle = 45, hjust = 1))
ggplot(data = movies, aes(x = genre, fill = audience_rating)) + geom_bar() + theme(axis.text.x=element_text(angle = 45, hjust = 1))
ggplot(data = movies, aes(x = genre, fill = audience_rating)) + geom_bar(position = "fill") + theme(axis.text.x=element_text(angle = 45, hjust = 1))
ggplot(data = movies, aes(x = genre, fill = audience_rating)) + geom_bar(position = "dodge") + theme(axis.text.x=element_text(angle = 45, hjust = 1))
theme_grey()?ggplot(data = movies, aes(x = genre, fill = audience_rating)) + geom_bar(position = "dodge") + theme_bw() + theme(axis.text.x = element_text(angle = 45, hjust = 1))
ggplot2 resourcesVisit http://docs.ggplot2.org/current/ for documentation on the current version of the ggplot2 package. It's full of examples!
Refer to the ggplot2 cheatsheet: http://www.rstudio.com/wp-content/uploads/2015/12/ggplot2-cheatsheet-2.0.pdf
Themes vignette: http://docs.ggplot2.org/dev/vignettes/themes.html
Recereate the following plot. Hint: Add a labs() layer.
A web application framework for R with which you can easily turn your analyses into interactive web applications
No HTML, CSS, or JavaScript knowledge required
install.packages("shiny")
library(shiny)
Shiny apps have two components:
a user-interface script: controls the layout and appearance of your app
a server script: contains the instructions that your computer needs to build your app
runExample("01_hello")